package com.ngovanhau.justsort;

import com.ngovanhau.justsort.control.ValueControl;
import com.ngovanhau.justsort.item.config.MyConfig;
import com.ngovanhau.justsort.sound.MusicBackground;
import com.ngovanhau.justsort.sound.Sound;
import com.ngovanhau.justsort.util.UtilFont;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainMenuActivity extends Activity {

	public MusicBackground music;
	public static Sound sound;
	MySharedPreferences mySharedPreferences;
	LinearLayout layout;

	Typeface font;
	Button buttonSound;

	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_demo_game);

		buttonSound = (Button) findViewById(R.id.sound);
		layout = (LinearLayout) findViewById(R.id.menu_layout);
		
		// put it to ValueControl
		mySharedPreferences = new MySharedPreferences(this);
		mySharedPreferences.getIsMusic();
		mySharedPreferences.getIsSound();
		mySharedPreferences.getCurrentLevel();
		mySharedPreferences.getCurrentType();
		mySharedPreferences.getUnlockType();
		mySharedPreferences.getRowLevel();
		mySharedPreferences.getColumnLevel();

		sound = new Sound();
		sound.loadSound(this);

		UtilFont.init(this);
		MyConfig.getDisplayScreen(this);
		
		float y = MyConfig.RACE_HEIGHT*40;
		layout.setY(y);
		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		sound = null;
		music.release();
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		music = new MusicBackground();
		music.loadMusic(this);
		music.play();
		changeIconMusicSound(true);
	}
	
	@Override
	protected void onStop(){
		super.onStop();
		music.pause();
	}

	public void onClick(View v) {
		sound.playClickButton();
		Intent intent = null;
		
		if(v.getId()== R.id.Play){
			intent = new Intent(this, LevelActivity.class);
			startActivity(intent);
		}else if(v.getId()== R.id.HowPlay){
			intent = new Intent(this, HelpActivity.class);
			startActivity(intent);
		}else if(v.getId()== R.id.Exit){
			this.finish();
		}else if(v.getId()== R.id.share){
			shareFacebook();
		}else if(v.getId()== R.id.sound){
			updateIconMusicSound();
		}
	}

	public void changeIconMusicSound(boolean check) {
		if (ValueControl.isMusic) {
			music.play();
			buttonSound.setBackgroundResource(R.drawable.menu_btn_nomute);
		} else {
			buttonSound.setBackgroundResource(R.drawable.menu_btn_mute);
		}
	}

	public void updateIconMusicSound() {
		if (ValueControl.isMusic) {
			mySharedPreferences.updateIsMusic(false);
			mySharedPreferences.updateIsSound(false);
			buttonSound.setBackgroundResource(R.drawable.menu_btn_mute);
			music.pause();
		} else {
			mySharedPreferences.updateIsMusic(true);
			mySharedPreferences.updateIsSound(true);
			buttonSound.setBackgroundResource(R.drawable.menu_btn_nomute);
			music.resume();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			 if(music != null){
				 music.release();
			 }
			this.finish();
		}
		return false;
	}
	
	public void shareFacebook(){
		Intent shareIntent = new Intent();
		shareIntent.setAction(Intent.ACTION_SEND);
		
		shareIntent.setType("text/plain");
		shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello justSort");
		startActivity(Intent.createChooser(shareIntent, "shared your thought"));
	}
}